﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Clannan.Helper;
using Spring.Context.Support;
using Spring.Context;
using Service.Interface;
using Domain;

namespace Clannan.WebService
{
    /// <summary>
    /// AutoSendMsgService 的摘要说明
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
    // [System.Web.Script.Services.ScriptService]
    public class AutoSendMsgService : System.Web.Services.WebService
    {
        [WebMethod]
        public string AutoSendMessage()
        {
            SendEmail sendEmail = new SendEmail();
            SendFetion sendFetion = new SendFetion();

            //spring获取对象
            IApplicationContext context = ContextRegistry.GetContext();
            IMemoService memoService = context.GetObject("Service.Memo") as IMemoService;

            if (memoService != null)
            {
                IList<Memo> memoList = memoService.LoadAllWarnMemo();
                foreach (Memo memo in memoList)
                {
                    if (memo.SendEmail == true)
                    {
                        try
                        {
                            sendEmail.Send(memo.UserInfo.Email, "来自Clannan的备忘录提醒", memo.Content);
                            memo.SendEmail = false;
                            memoService.Update(memo);
                        }
                        catch { }
                    }
                    if (memo.SendMobileMessage == true)
                    {
                        try
                        {
                            sendFetion.Send(memo.UserInfo.Mobile, "备忘录提示:" + memo.Content);
                            memo.SendMobileMessage = false;
                            memoService.Update(memo);
                        }
                        catch { }
                    }
                }
            }
            return "success";
        }
    }
}